<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>仓库管理</title>
<%@ include file="/WEB-INF/pages/common/frameCommonJs.jsp"  %>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/commonService.js'></script>
<script>
   var getClassUrl = "<%=request.getContextPath()%>/warehouse/getAllWareHouseClassJson.action";
   $(function(){
   		$('.icon-reload').click(
			function (){ 
				$('#wcTree').tree({url:'<%=request.getContextPath()%>/warehouse/getAllWareHouseClassJson.action'});
				$('#wcTree').tree('reload')
			}
		);
		$(".icon-reload").css("cursor","pointer");
		$('#wcTree').tree({
			animate: true,
			url: getClassUrl,
			onBeforeExpand:function(node){
				$('#wcTree').tree("options").url=getClassUrl + "?wc.id=" + node.id;
			},
			onClick:function(node){
				var node = $('#wcTree').tree('getSelected');
				$('#wars').datagrid('reload',{wareClassId:node.id});
				$('#wcTree').tree('expand',node.target);
				$('#wars').datagrid({pageNumber:1});// 将表格默认设为1
				$('#wcId').val(node.id);//点击商品类别的时候，将当前类别Id设置到查询表单中
			},
			onContextMenu: function(e, node){
				e.preventDefault();
				$('#wcTree').tree('select', node.target);
				$('#mm').menu('show', {
					left: e.pageX,
					top: e.pageY
				});
			}
			
		});
		// 右侧表单内容
		$('#wars').datagrid({
				fit:true,
				nowrap: false,
				striped: true,
				collapsible:true,
				url:'<%=request.getContextPath()%>/warehouse/getWarsByWarClassJson.action',
				sortName: 'code',
				sortOrder: 'desc',
				remoteSort: false,
				idField:'code',
				singleSelect:true,
				title:'仓库列表',
				columns:[[
					{field:'name',title:'名称',width:120},
					{field:'wareClassName',title:'类别',width:120},
					{field:'no',title:'编号',width:120},
					{field:'note',title:'备注',width:120}
				]],
				pageSize:15,  
                pageList:[5,10,15,20],  
                fitColumns: true,  
				pagination:true,
				rownumbers:true
			});
   });
   // 新增往来单位
   function newWar(){
   		/*var node = $('#wcTree').tree('getSelected');
   		 if(node==null){
   			$.messager.alert("提示信息:","请选择类别!");
   			return;
   		} */
		var title = "新增仓库信息";
   		$('#dlg').dialog('open').dialog('setTitle',title);  
	    $('#fm').form('clear');  
	    url = '<%=request.getContextPath()%>/warehouse/saveWareHouseJson.action'; 
	    //$("#w\\.wc\\.id").val(node.id);
   }
   function editWar(){
   		var node = $("#wars").datagrid('getSelected');
   		var title = "修改【" + node.name + "】仓库信息";
   		$('#dlg').dialog('open').dialog('setTitle',title);
   		$("#w\\.id").val(node.id);
   		$("#w\\.name").val(node.name);
   		$("#w\\.no").val(node.no);
   		$("#w\\.note").val(node.note);
   		$("#w\\.wareClassName").val(node.wareClassName);
	    url = '<%=request.getContextPath()%>/warehouse/updateWareHouseJson.action'; 
   }
   function saveWar(){  
	    $('#fm').form('submit',{  
	        url: url,  
	        onSubmit: function(){  
	        	var whClass = $("#w\\.wareClassName").val();
	        	if(whClass.length == 0){
	        		$.messager.alert("提示信息","请选择仓库分类!",'warning');
	        		return false;
	        	}
	            return $(this).form('validate');  
	        },  
	        success: function(result){  
	       		var result = eval('('+result+')');
	        	// 重读该分类
	        	$("#wars").datagrid('reload');
	        	var msg = result.name;
	        	if($("#wc\\.id").val()!=""){
	        		msg = "【" + msg + "】修改成功!";
	        	}else{
	        		msg = "【" +  msg + "】新增成功!";
	        	}
            	$.messager.show({title:"提示信息",msg:msg,showType:'slide'});
                $('#dlg').dialog('close');      // close the dialog  
	        }  
	    });  
	} 
	function deleteWH(){
   		var node = $('#wars').datagrid('getSelected');
   		var condt = new delOrgProWhCondtn();
   		condt.whid = node.id;
   		commonService.valDelOrgProWH(condt,function(data){
   			if(data=="no"){
				$.messager.alert("提示信息","该仓库已在业务单据中使用，不能删除!",'warning');
				return;
	        }
   			$.messager.confirm('删除仓库信息', '确认删除【' + node.name + '】吗?', function(r){
				if(r){
					$.ajax({
						type:"post",
						data:"w.id=" + node.id,
						url:"<%=request.getContextPath()%>/warehouse/delWH.action",
						success:function(result){
							var data = eval('(' + result + ')');
							$.messager.show({title:"提示信息",msg:"删除成功",showType:'slide'});
							$('#wars').datagrid('reload');
						}
					});
					
				}
			});
   		});
   }
	
  function selectWc(){
	var node = $('#swcTree').tree('getSelected');
	$("#w\\.wareClassName").val(node.text);
	$("#w\\.wc\\.id").val(node.id);
	$('#wcDlg').dialog('close');
}
</script>
</head>
<body class="easyui-layout" >
<div region="west" iconCls="icon-reload" title="仓库类别" split="true" style="width:180px;">
		<ul id="wcTree"></ul>
</div>
<div region="center" title="仓库信息管理" style="overflow:hidden;">
<div class="easyui-layout" data-options="fit:true">
	<div style="height:31px" data-options="region:'north',border:false">
		<div id="tb" class="datagrid-toolbar">  
		    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:newWar()">新增</a>  
		    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="javascript:editWar()">修改</a>  
		    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:deleteWH()">删除</a>  
		</div>
	</div>
	<div data-options="region:'center',border:false">
		<table id="wars"></table>
	</div>
</div>
<div id="dlg" class="easyui-dialog" style="width:340px;height:250px;"  
        closed="true" buttons="#wh-dlg-buttons">  
    <form id="fm" method="post"> 
    <table border="0" >
            <tr>
            	<td align="right">名称:</td>
            	<td align="left">
            	 <input type="text" id="w.name" name="w.name"  class="easyui-validatebox"  required="true" />
            	 <font color="red">*</font>
            	</td>
            	<input type="hidden" id="w.id" name="w.id" value="" />
            	<input type="hidden" id="w.wc.id" name="w.wc.id" value="" />
            	<input type="hidden" id="wareClassId" name="wareClassId" value="" />
            	<input type="hidden" id="wcId" name="wcId" value="" />
            </tr> 
            <tr>
            	<td align="right">仓库分类:</td>
            	<td align="left">
            		<input type="text" name="w.wareClassName" id="w.wareClassName" class="easyui-validatebox" readonly="readonly" onclick="openSelectWC()" style="PADDING-LEFT: 20px;BACKGROUND-IMAGE: url(<%=request.getContextPath()%>/images/note_add.png); BACKGROUND-REPEAT: no-repeat;cursor:pointer;" size="17" /> 
            		<font color="red">*</font>
            		<input type="hidden" name="w.wareClassId" id="w.wareClassId" class="easyui-validatebox"  />
            	</td>
            </tr>
            <tr>
            	<td align="right">自定义编号:</td>
            	<td align="left"><input type="text" id="w.no" name="w.no" required="true" /></td>
            </tr>  
            <tr>
            	<td align="right">备注:</td>
            	<td align="left"><textarea id="w.note" name="w.note" ></textarea></td>
            </tr>
    </table>  
    </form>
</div>
<div id="wh-dlg-buttons">  
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveWar()">保存</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>  
</div>   
<%@ include file="/WEB-INF/pages/common/wareHouseClassSelect.jsp"  %>
</body>
</html>